var api = require('../../utils/api.js');
var app = getApp();


Page({

    /**
     * 页面的初始数据
     */
    data: {
        avatar:'',
        nickname:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onShow(options) {

        var that=this;

        api.post({
            url: 'user/index',
            data: {},
            success: data => {
                console.log(data);
                that.setData({
                    avatar:data.data.avatar,
                    nickname:data.data.nickname
                })

            }
        })


    },

    chooseImage(){
        let that = this;
        var usrl=api.HOST;

        wx.chooseMedia({
          count: 1,
          mediaType:['image'],
          success: function (res) {
            let tempFilePaths = res.tempFiles;
         
            wx.uploadFile({
              filePath: tempFilePaths[0].tempFilePath,
              name: 'file',
              url: usrl+'common/upload',
              formData:{},
              success:function(res){
                var data= JSON.parse(res.data);
                    that.setData({
                        avatar:data.data.fullurl
                    });
        
              },
              fail: function (res) {
                console.log('上传失败');
                }
            })
          }
        })
    },

    getvalue(ret){
        this.setData({
            nickname:ret.detail.value
        })
    },

    submits(){
        var avatar=this.data.avatar;
        var nickname=this.data.nickname;
        var that=this;
        api.post({
            url: 'user/setuserinfo',
            data: {avatar:avatar,nickname:nickname},
            success: data => {
                console.log(data);
            }
        })
    }

})